<!-- 会签单位选择 多选 -->
<template>
  <view>
    <van-action-sheet v-model="showCompo" :close-on-click-overlay="false" @closed="closeSheet"  title="会签单位" cancel-text="确定">
      <van-list class="list-item" v-model="loading" :finished="finished" @load="load">

        <van-grid :column-num="3" :border="false" :center="false" >
          <van-checkbox-group v-model="result" direction="horizontal" >
            <van-grid-item v-for="(item,index) in counterSignList">
              <van-checkbox  :name="item.name" icon-size="34rpx" :key="item.id" :disabled="item.disable">
                {{item.name}}
              </van-checkbox>
            </van-grid-item>
          </van-checkbox-group>
        </van-grid>

        <!--				<van-checkbox-group v-model="result">
                  <van-cell-group>
                    <van-cell v-for="(item, index) in counterSignList"  :key="index" :title="item.name"
                      @click="toggle(index)">
                      <template #right-icon>
                        <van-checkbox :name="item.name" :disabled="item.disable" :label-disabled="item.disable" ref="checkboxes" />
                      </template>
                    </van-cell>
                  </van-cell-group>
                </van-checkbox-group>-->
      </van-list>
    </van-action-sheet>
  </view>
</template>

<script>

export default{
  props: {
    showSignOrg:{
      type: Boolean,
      default: true
    },
    //公文的extendIdForRelate
    fwId:{
      type:String,
      default:''
    },
    //选中的会签单位
    checkedVal:{
      type:String,
      default:''
    }
  },
  watch: {
    showSignOrg(val){
      this.showCompo = val
    },
    //选中的值回显
    checkedVal: {
      handler (nVal, oVal) {
        if(this.util.isNotEmpty(nVal)){
          this.result=nVal.split(',')
        }
      },
      immediate: true,
      deep: true
    },
    fwId:{
      handler (nVal, oVal) {
        if(this.util.isNotEmpty(nVal)){
          this.initHuiqianSelected(nVal)
        }
      },
      immediate: true,
      deep: true
    },
  },
  data(){
    return{
      showCompo:false,//是否显示组件
      show:true,
      result:[],
      loading: false,
      finished: true, //不显示加载中
      isDisabled:true,
      countersignValue:[],
      counterSignList: [
        // {
        //     "name": "航安办",
        //     "id": "002001007",
        //     "sort": 6,
        //     "value": "002001007_航安办_4131"
        // }
      ],
      huiqianSelected:[]
    }
  },
  mounted() {
    this.getCountersignGlj()
  },

  methods: {
    closeSheet(){
      //this.result:[{"name":"航安办","id":"002001007","sort":6,"value":"002001007_航安办_4131"},{"name":"法规处","id":"002001008","sort":9,"value":"002001008_法规处_10257"}]
      if(this.result.length>0){//数组不为空则进行会签单位添加
        this.result.forEach((item)=>{
          let arr = this.counterSignList.filter( t => t.name == item)
          if(arr.length > 0) {
            this.countersignValue.push(arr[0].value)
          }
        })
        this.updateCountersigning(this.countersignValue.toString())
      }
      this.$emit('closeSignOrgSheet',this.result)
      this.countersignValue=[]//清空，放着多次调用追加
    },
    load() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      // setTimeout(() => {
      // 	// for (let i = 0; i < 10; i++) {
      // 	// 	this.newsList.push(this.newsList.length + 1);
      // 	// }

      // 	// 加载状态结束
      // 	this.loading = false;

      // 	// 数据全部加载完成
      // 	if (this.newsList.length >= 5) {
      // 		this.finished = true;
      // 	}
      // }, 100);
    },
    //
    // toggle(index) {
    //   // console.log("index: " + JSON.stringify(index));
    // 	this.$refs.checkboxes[index].toggle();
    // },

    //获取已选择的会签单位
    initHuiqianSelected(fwId){
      var _this = this
      this.$httploading({
        url: this.$httploading.adornUrl(`/caac/oa/mhjcomponent/countersign/countersignController/initHuiqianSelected`),
        method: 'get',
        params: this.$httploading.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
          'fwId':fwId
        })
      }).then(({data}) => {
        //[{"flag":"couldDelete","name":"航安办","id":"002001007","sort":6,"value":"002001007_航安办_4131"},
        // {"flag":"couldNotDelete","name":"法规处","id":"002001008","sort":9,"value":"002001008_法规处_10257"}]
        if(data.code==200){
          this.huiqianSelected=data.data
          this.result=[]
          this.huiqianSelected.forEach((item)=>{
            this.result.push(item.name)//回显
            this.counterSignList.forEach((v,index)=>{
              if(item.flag == 'couldDelete' && item.id == v.id){
                this.counterSignList[index]["disable"]=false
              }else if(item.flag == 'couldNotDelete' && item.id == v.id){
                this.counterSignList[index]["disable"]=true
              }else{
                this.counterSignList[index]["disable"]=false
              }
            })
          })
        }else{
          console.log(data);
        }

      })
    },

    //获取会签单位选择列表
    getCountersignGlj(){
      var _this = this
      this.$httploading({
        url: this.$httploading.adornUrl(`/caac/oa/authority/gljfw/getCountersignGlj`),
        method: 'get',
        params: this.$httploading.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
        })
      }).then(({data}) => {
        // console.log("获取会签单位选择列表: " + JSON.stringify(data));
        if(data.code==200){
          _this.counterSignList = data.data
          // _this.$toast(JSON.stringify(_this.orgList))
        }else{
          console.log(data);
        }

      })
    },

    //添加会签单位
    updateCountersigning(countersignValue){
      // console.log("fwId: " + JSON.stringify(this.fwId));
      var _this = this
      this.$http({
        url: this.$http.adornUrl(`/caac/oa/mhjcomponent/countersign/countersignController/updateCountersigning`),
        method: 'POST',
        params: this.$http.adornParams({
          'systemKey': this.cache.get('userInfo').systemKey,//固定值
          'systemFlag': this.cache.get('userInfo').systemFlag,//固定值
          'fwId': this.fwId,//公文的extendIdForRelate
          'countersignValue':countersignValue
        })
      }).then(({data}) => {
        console.log("添加会签单位: " + JSON.stringify(data));
        if(data.code==200){
          // this.$toast('添加成功')

        }else{
          console.log(data);
        }

      })
    },

  }
}
</script>

<style>
.van-action-sheet{
  z-index: 99999;
  min-height: 80%;
}
/* .list-item{
  margin-bottom: 100rpx;
} */
/* .btn-bottom{
  background-color: #FFFFFF;
  height: 100rpx;
  border-top: 10rpx solid #f7f8fa;
  position: fixed;
  bottom: 0;
  width: 100%;
} */
</style>
